import java.util.Arrays;
import java.util.Scanner;

// 学生管理系统
class StudentSystem {
    Student[] students;
    public int size = 0;
    private int DEFAULT_CAPACITY;

    public StudentSystem() {
        students = new Student[10];
        this.DEFAULT_CAPACITY = 10;
    }

    // 插入函数.
    public void add(Student student) {
        if(this.size == this.DEFAULT_CAPACITY) {
            // 扩容
            this.students = Arrays.copyOf(this.students,this.DEFAULT_CAPACITY * 2);
            this.DEFAULT_CAPACITY = this.DEFAULT_CAPACITY * 2;
        }
        this.students[size] = student;
        this.size++;
    }
}

// 学生类.
class Student {
    private String IdCard; // 身份证
    private String studentID; // 学号
    private String name;
    private double score;

    public Student(String idCard, String studentID, String name, double score) {
        this.name = name;
        this.score = score;
        this.studentID = studentID;
        this.IdCard = idCard;
    }

    public String getIdCard() {
        return IdCard;
    }

    public void setIdCard(String idCard) {
        IdCard = idCard;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        menu();
    }

    private static void menu() {
        Scanner scanner = new Scanner(System.in);
        StudentSystem studentSystem = new StudentSystem();


        int input;
        do {
            System.out.println("1. 存储学生信息       2. 查询学生信息");
            System.out.println("3. 按学号排序或者成绩排序      4. 插入一个学生信息");
            System.out.println("5. 根据身份证或学号删除学生信息.      6. 退出系统");

            System.out.print("请选择要执行的操作: ");
            input = scanner.nextInt();
            switch (input) {
                case 1:
                    Student student = add();
                    studentSystem.add(student);
                    break;
                case 2:
                    search(studentSystem);
                    break;
                case 3:
                    sort(studentSystem);
                    Print(studentSystem);
                    break;
                case 4:
                    Student student2 = add();
                    studentSystem.add(student2);
                    scoreSOrt(studentSystem);
                    Print(studentSystem);
                    break;
                case 5:
                    delete(studentSystem);
                    Print(studentSystem);
                    break;
                case 6:
                    System.out.println("已退出系统!");
                    break;
                default:
                    System.out.println("输入错误!请重新输入");
            }
        } while(input != 6);
    }

    private static void delete(StudentSystem studentSystem) {
        Scanner scanner = new Scanner(System.in);

       while (true) {
           System.out.println("1. 根据学号删除         2. 根据身份证号删除");
           System.out.print("请输入您的选择: ");
           int input = scanner.nextInt();
           if(input == 1) {
               // 按学号
               System.out.print("请输入学生学号: ");
           } else if(input == 2) {
               // 按身份证号:
               System.out.println("请输入学生身份证号: ");
           } else {
               System.out.println("选择错误!请重新选择!");
               continue;
           }
           String str = scanner.next();
           int i = 0;
           for (; i < studentSystem.size; i++) {
               if(studentSystem.students[i].getStudentID().equals(str)
                       || studentSystem.students[i].getIdCard().equals(str))
                   break;
           }
           if(i >= studentSystem.size) {
               System.out.println("该学生不存在!");
           } else {
               if (studentSystem.size - 1 - i >= 0)
                   System.arraycopy(studentSystem.students, i + 1, studentSystem.students, i,
                           studentSystem.students.length - 1 - i);
               studentSystem.size--;
           }
           break;
       }
    }

    private static void Print(StudentSystem studentSystem) {
        System.out.println("排序后的全体学生信息如下: ");
        for (int i = 0; i < studentSystem.size; i++) {
            Student student = studentSystem.students[i];
            System.out.println("姓名: " + student.getName() + "    成绩: " + student.getScore() +
                    "   学号: " + student.getStudentID() + "   身份证号: " + student.getIdCard());
        }
        System.out.println();
    }

    private static void sort(StudentSystem studentSystem) {
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("1. 按学号从小到大排序         2. 按成绩从大到小排序");
            System.out.print("请输入您的选择: ");
            int input = scanner.nextInt();
            if(input == 1) {
                // 按学号排序
                IdSort(studentSystem);
                break;
            } else if(input == 2) {
                // 按成绩排序
                scoreSOrt(studentSystem);
                break;
            } else {
                System.out.println("选择错误!请重新选择!");
            }
        }
    }

    private static void IdSort(StudentSystem studentSystem) {
        for(int i = 0; i < studentSystem.size; ++i) {
            boolean flag = true;
            for(int j = 0; j < studentSystem.size - 1 - i; ++j) {
                if(studentSystem.students[j].getStudentID().compareTo(studentSystem.students[j+1].getStudentID()) < 0) {
                    Student tmp = studentSystem.students[j];
                    studentSystem.students[j] = studentSystem.students[j+1];
                    studentSystem.students[j+1] = tmp;
                    flag = false;
                }
            }
            if(flag)
                break;
        }
    }

    private static void scoreSOrt(StudentSystem studentSystem) {
        for(int i = 0; i < studentSystem.size; ++i) {
            boolean flg = true;
            for(int j = 0; j < studentSystem.size - 1 - i; ++j) {
                if(studentSystem.students[j].getScore() < studentSystem.students[j+1].getScore()) {
                    Student tmp = studentSystem.students[j];
                    studentSystem.students[j] = studentSystem.students[j+1];
                    studentSystem.students[j+1] = tmp;
                    flg = false;
                }
            }
            if(flg)
                break;
        }
    }

    private static void search(StudentSystem studentSystem) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. 使用身份证号查询         2. 使用学号查询");
            System.out.print("请输入您的选择: ");
            int input = scanner.nextInt();
            if(input == 1) {
                // 按身份证号查询.
                System.out.print("请输入学生身份证号: ");
            } else if(input == 2) {
                System.out.print("请输入学生学号: ");
            } else {
                System.out.println("选择错误!请再次选择.");
                continue;
            }

            String str = scanner.next();
            int i = 0;
            for(; i < studentSystem.size; ++i) {
                if(studentSystem.students[i].getIdCard().equals(str))
                    break;
            }
            if(i >= studentSystem.size )
                System.out.println("该学生不在系统中!");
            else {
                Student student = studentSystem.students[i];
                System.out.println("学生信息如下: ");
                System.out.println("姓名: " + student.getName() + "    成绩: " + student.getScore() +
                        "   学号: " + student.getStudentID() + "   身份证号: " + student.getIdCard());
                System.out.println();
            }
            break;
        }

    }

    private static Student add() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入学生身份证号: ");
        String Id = scanner.next();
        System.out.print("请输入学生学号: ");
        String studentID = scanner.next();
        System.out.print("请输入学生姓名: ");
        String name = scanner.next();
        System.out.print("请输入学生成绩: ");
        double score = scanner.nextDouble();

        return new Student(Id,studentID,name,score);
    }
}
